<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/dute_favicon_32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/dute_favicon_16x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <link rel="manifest" href="/images/manifest.json">
  <meta name="msapplication-config" content="/images/browserconfig.xml">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="google-site-verification" content="mpI5dkydstZXl6UcDCppqktXK0bbvqdZ6LkZ3KNk4Iw">
  <meta name="baidu-site-verification" content="code-a1LksZX2Ds">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"whitestore.top","root":"/","scheme":"Gemini","version":"7.8.0","exturl":true,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="深入理解JVM的学习和使用">
<meta property="og:type" content="article">
<meta property="og:title" content="深入理解JVM - 分代的基本概念">
<meta property="og:url" content="https://whitestore.top/2021/08/17/202201282354/index.html">
<meta property="og:site_name" content="爱看书的阿东">
<meta property="og:description" content="深入理解JVM的学习和使用">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210708074839.png">
<meta property="article:published_time" content="2021-08-17T14:54:19.000Z">
<meta property="article:modified_time" content="2023-07-16T06:28:09.269Z">
<meta property="article:author" content="阿东">
<meta property="article:tag" content="深入理解JVM">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210708074839.png">

<link rel="canonical" href="https://whitestore.top/2021/08/17/202201282354/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>深入理解JVM - 分代的基本概念 | 爱看书的阿东</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="爱看书的阿东" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">爱看书的阿东</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">赐他一块白色石头，石头上写着新名</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i>站点地图</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL2xhenlUaW1lcw==" title="Follow me on GitHub" aria-label="Follow me on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://whitestore.top/2021/08/17/202201282354/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="阿东">
      <meta itemprop="description" content="随遇而安">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="爱看书的阿东">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          深入理解JVM - 分代的基本概念
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-08-17 22:54:19" itemprop="dateCreated datePublished" datetime="2021-08-17T22:54:19+08:00">2021-08-17</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2023-07-16 14:28:09" itemprop="dateModified" datetime="2023-07-16T14:28:09+08:00">2023-07-16</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/JVM/" itemprop="url" rel="index"><span itemprop="name">JVM</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/08/17/202201282354/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/08/17/202201282354/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>5.1k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>5 分钟</span>
            </span>
            <div class="post-description">深入理解JVM的学习和使用</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="深入理解JVM-分代的基本概念"><a href="#深入理解JVM-分代的基本概念" class="headerlink" title="深入理解JVM - 分代的基本概念"></a>深入理解JVM - 分代的基本概念</h1><h1 id="目录"><a href="#目录" class="headerlink" title="目录"></a>目录</h1><p>[TOC]</p>
<h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>​    本次讲述jvm分代模型的基础概念，这个专栏会由浅入深的不断构建起来，循序渐进，和上一篇一样，是非常基础的内容</p>
<p>​    其实这一篇才是对象分配的内容：<span class="exturl" data-url="aHR0cHM6Ly9qdWVqaW4uY24vcG9zdC82OTcwNTU3OTc4ODMxMzg4NzA5" title="https://juejin.cn/post/6970557978831388709">深入理解JVM虚拟机 - jvm的对象分配策略<i class="fa fa-external-link"></i></span> 会发现里面有很多东西需要消化，而这一篇会讲一些基础的内容。</p>
<h1 id="概述："><a href="#概述：" class="headerlink" title="概述："></a>概述：</h1><ol>
<li>讲述JVM的基础分代模型以及版本升级的处理。</li>
<li>对象分配的基础概念和知识。</li>
<li>长期存活的对象是如何躲过垃圾回收进入到老年代</li>
<li>jvm内存的核心参数解释以及基础配置</li>
</ol>
<a id="more"></a>

<h2 id="JVM的分代模型"><a href="#JVM的分代模型" class="headerlink" title="JVM的分代模型"></a>JVM的分代模型</h2><p>​    JVM的基础分代模型：年轻代，老年代，永久代。这里需要注意内存模型通常是和垃圾回收期相辅相成的，现代的垃圾收集器已经十分复杂了，甚至已经没有了分代的概念，我们所讲的新生代老年代是最初设计的一些理念，很多人学到更为先进的垃圾收集器可能会蒙蔽，比如堆内存怎么变成一块一块的了，以前不是说堆内存只有新生代和老年代这两块完整大的空间呢？</p>
<ul>
<li><p>年轻代：大部分的对象都是朝生夕灭的。同时JVM总是把对象优先分配在新生代，并且新生代触发的垃圾回收通常被称为 Minor GC。</p>
</li>
<li><p>老年代：属于长期存活的对象贮存地区，由新生代晋升而来。老年代在通常情况下占有堆中最大的一块内存空间。老年代触发的垃圾回收通常被称为 <strong>Full GC</strong>。</p>
</li>
<li><p>永久代：注意永久代不等同于方法区，主要存放一些静态常量或者存放.class类信息，方法区是可以被垃圾回收的，但是触发的条件<strong>十分苛刻</strong>，同时里面最常用的<strong>常量池</strong>以及在JDK8之后挪到了堆中，将永久代并且改名为元空间。</p>
<p>为什么要把老年代拉进来讲呢？其实永久代的设计是一个失败的设计，JDK8废弃永久代同时用使用本地内存的元空间来替代，大大减少了永久代溢出的可能，因为已经不使用虚拟机的内存了，而是直接使用本机的内存存储，还有一个好处是永久代也不会再去抢堆内存了。</p>
</li>
</ul>
<h2 id="对象分配的基础了解"><a href="#对象分配的基础了解" class="headerlink" title="对象分配的基础了解"></a>对象分配的基础了解</h2><p>​    下面基础了解一下对象分配的基础概念，这些概念可能在学习JAVA的时候就已经接触过了，所以也都是简单提一下：</p>
<ol>
<li><p>大部分正常对象优先在新生代分配。在垃圾回收线程开启之后，会将长期存活对象晋升到老年代存储</p>
</li>
<li><p>即使是静态变量存在于方法区当中的对象，实例对象也是在堆中分配。但是只有被栈帧局部变量使用的时候才会触发<strong>初始化</strong>。</p>
</li>
<li><p>在堆上分配的依然优先选择在新生代分配，当长期存活之后会晋升老年代长久贮存内存。</p>
</li>
<li><p><strong>当新生代的内存空间占满的时候，会触发minor</strong> <strong>gc</strong>。老年代空间被占满之后会触发<strong>Full GC</strong>，同时Full GC</p>
</li>
</ol>
<p>下面用一张图解释对象分配的基础概念：</p>
<p>​    我们以下的代码为例简单讲解对象分配的方式。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">OneWeek</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Properties properties = <span class="keyword">new</span> Properties();</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        InputStream resourceAsStream = OneWeek.class.getClassLoader().getResourceAsStream("app.properties");</span><br><span class="line">        properties.load(resourceAsStream);</span><br><span class="line">        System.out.println(<span class="string">"load properties user.name = "</span> + properties.getProperty(<span class="string">"user.name"</span>));</span><br><span class="line">    &#125;<span class="comment">/*运行结果：</span></span><br><span class="line"><span class="comment">    load properties user.name = 123</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    #app.properties:</span></span><br><span class="line"><span class="comment">    user.name=123</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210708074839.png" alt="对象分配的细节"></p>
<p>​    首先，当线程开启的时候，首先会加载并且初始化<strong>OneWeek.class</strong>对象，同时将Main()方法压入到虚拟机栈中，同时创建栈帧以及局部变量表等内容。</p>
<p>​    然后，执行字节码引擎执行字节码里面的指令，根据代码可以看到，方法首先会拿到当前类的class文件，并且调用当前类加载器加载<code>app.properties</code>这个文件到内存当中，注意在加载的过程中会创建char[]数组存储加载的内容，以及创建文件IO流读取文件等操作，这部分的对象都是<strong>优先分配在新生代</strong>的。</p>
<p>​    当程序计数器执行到：<code>properties.load(resourceAsStream);</code>这一行代码对应的字节码指令的时候，会发现Properties.class没有加载，同时又发现他是一个静态对象，所以会把当前的对象引用分配到方法区进行贮存，注意方法区存放的是对象的引用不是对象的实例，<strong>实例依旧优先分配在新生代</strong>。但是这里为什么直接划分到老年代了呢？因为我们知道这个静态常量如果被其他的类引用，那么可以算作是长期存活对象，那么长期存活的对象迟早是要进入到老年代的，所以图中直接划分到老年代了。</p>
<p>​    同时我们也可以发现如果新生代在垃圾回收之后存在长期存活的对象，会在垃圾回收之后自动晋升到老年代进行存储。</p>
<p>​    特别要注意我们平时new出来的对象都是<strong>强引用</strong>。哪怕是栈帧局部变量<strong>只被使用过一次</strong>对象的引用随着栈帧回收，也是<strong>不会立马回收</strong>的，而是要等到垃圾回收线程开启之后被回收掉。</p>
<h2 id="长期存活的对象会躲过垃圾回收"><a href="#长期存活的对象会躲过垃圾回收" class="headerlink" title="长期存活的对象会躲过垃圾回收"></a>长期存活的对象会躲过垃圾回收</h2><ol>
<li>如果对象年龄到达15岁，会自动进入到老年代。</li>
<li>大对象进入新生代如果无法容纳，也会直接进入老年代。</li>
<li>老年代也会触发垃圾回收，会把没有人引用的垃圾对象清理掉</li>
<li>在满足某些垃圾回收器晋升机制或者满足一些特定条件的情况下，新生代对象会提前进入老年代。</li>
</ol>
<p>​    下面分别说明一下这四个点是如何来的：</p>
<p>​    什么是<strong>对象年龄</strong>？对象年龄就是在JVM运行的时候，新生代中的对象只要每躲过一次垃圾回收，内部的引用计数器就会把当前年龄的对象+1，当对象的年龄累加到15之后，该对象在下一次垃圾回收之后就会晋升到老年代。当然此时并不是高枕无忧了，当老年代也被占满的时候如果当前对象已经没有被GC ROOT引用了，也还是会被当做垃圾回收的。</p>
<p>​    大对象最典型的案例就是<strong>大字符串</strong>或者很大的<strong>字节数组</strong>，因为需要占用 <strong>连续的内存空间</strong>，如果新生代无法容纳，那么毫无疑问是需要老年代作为兜底放到老年代直接存放，至于具体参数后续的文章会一一解释，这里了解基础概念即可。</p>
<p>​    老年代什么时候会触发垃圾回收的操作？条件毫无疑问是老年代放不下对象了，那么老年代为什么会满的，上一段我们说过老年代的对象都是从新生代来的，所以毫无疑问是新生代来到老年代发现老年代放不下了，所以老年代此时就会进行垃圾回收了，老年代的回收叫做Full GC。</p>
<p>​    看完上面这些，我们需要考虑的是 <strong>新生代进入老年代</strong>的时机，为什么要考虑这个东西，我们来分析一下：</p>
<p>​    首先是大对象，大对象进入新生代发现新生代放不下，如果老年代也发现放不下就直接Full GC了？这未免也太悲观了，万一垃圾回收之后放下来了，那不是白白浪费性能，不合适。其次，新生代一定要等到自己满了才进入老年代么，这样未免又太乐观了，因为万一新生代总有一些存活对象活在“等待区”（survior区）又不肯进入老年代，中间赖着不走，那么这一片区域反而失去了他的价值，所以也是不合适的，不如提前进入老年代。</p>
<h2 id="jvm内存的核心参数："><a href="#jvm内存的核心参数：" class="headerlink" title="jvm内存的核心参数："></a>jvm内存的核心参数：</h2><p>​    分代的核心参数如下，需要注意的是要注意<strong>区分大小写</strong>，输错会导致参数不生效：</p>
<ul>
<li><p><code>-Xms</code>：java堆内存的大小</p>
</li>
<li><p><code>-Xmx</code>：java堆内存的最大大小</p>
</li>
<li><p><code>-Xmn</code>：java堆当中的新生代大小，扣除新生代剩下就是老年代的内存大小</p>
</li>
<li><p><code>-XX:PermSize</code>：永久代大小<strong>（JDK8废弃，被替换为：-XX:MetaspaceSize）</strong></p>
</li>
<li><p><code>-XX:MaxPermSize</code>：永久代最大大小<strong>（JDK8废弃，被替换为：-XX:MaxMetaspceSize）</strong></p>
</li>
<li><p><code>-Xss</code>：每个线程栈内存大小</p>
</li>
</ul>
<blockquote>
<p>-xms和-xmx用来限定java堆的总大小以及扩张的最大大小，但是通常会设置为<strong>一样的参数</strong>，因为扩容需要<code>stop world</code>极大的影响系统性能。</p>
<p>-Xmn：是<strong>新生代</strong>的空间大小，老年代会自动根据<strong>总的堆大小 - 新生代</strong>大小算出来。</p>
<p>-xx:permsize和-xx:maxpermsize。会限制永久代大小和最大的大小，通常情况下设置为256M够用</p>
<p>- Xss 参数限制 每一个线程的<strong>栈内存</strong>大小。其实就是每一个线程对应虚拟机栈的大小，注意这区域不能太大，当然也不能太小。</p>
</blockquote>
<ul>
<li>注意到了jdk1.8之后，这两个参数被替换为<code>-XX:MetaspaceSize</code>     以及 <code>-XX:MaxMetaspceSize</code>。</li>
</ul>
<h3 id="启动参数的设置？"><a href="#启动参数的设置？" class="headerlink" title="启动参数的设置？"></a>启动参数的设置？</h3><p>​    在IDEA当中的启动参数设置如下：</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">-Xms1024m</span></span><br><span class="line"><span class="attr">-Xmx2048m</span></span><br><span class="line"><span class="meta">-XX</span>:<span class="string">ReservedCodeCacheSize=500m</span></span><br><span class="line"><span class="meta">-XX</span>:<span class="string">+UseConcMarkSweepGC</span></span><br><span class="line"><span class="meta">-XX</span>:<span class="string">SoftRefLRUPolicyMSPerMB=50</span></span><br><span class="line"><span class="attr">-ea</span></span><br><span class="line"><span class="meta">-XX</span>:<span class="string">CICompilerCount=2</span></span><br><span class="line"><span class="meta">-Dsun.io.useCanonPrefixCache</span>=<span class="string">false</span></span><br><span class="line"><span class="meta">-Djava.net.preferIPv4Stack</span>=<span class="string">true</span></span><br><span class="line"><span class="meta">-Djdk.http.auth.tunneling.disabledSchemes</span>=<span class="string">""</span></span><br><span class="line"><span class="meta">-XX</span>:<span class="string">+HeapDumpOnOutOfMemoryError</span></span><br><span class="line"><span class="meta">-XX</span>:<span class="string">-OmitStackTraceInFastThrow</span></span><br><span class="line"><span class="meta">-Djdk.attach.allowAttachSelf</span>=<span class="string">true</span></span><br><span class="line"><span class="meta">-Dkotlinx.coroutines.debug</span>=<span class="string">off</span></span><br><span class="line"><span class="meta">-Djdk.module.illegalAccess.silent</span>=<span class="string">true</span></span><br></pre></td></tr></table></figure>



<h3 id="Tomcat和spring-boot启动的时候参数如何设置"><a href="#Tomcat和spring-boot启动的时候参数如何设置" class="headerlink" title="Tomcat和spring boot启动的时候参数如何设置"></a>Tomcat和spring boot启动的时候参数如何设置</h3><ol>
<li><p>Tomcat在bin的<strong>Catalina</strong> 下面进行参数配置。</p>
</li>
<li><p>Spring boot 直接在vm.options里面加入虚拟机参数即可</p>
</li>
</ol>
<h2 id="方法区的对象到底会不会被回收？"><a href="#方法区的对象到底会不会被回收？" class="headerlink" title="方法区的对象到底会不会被回收？"></a>方法区的对象到底会不会被回收？</h2><ul>
<li><p>首先该类的<strong>所有实例对象</strong>都已经被回收</p>
</li>
<li><p>加载该类的<strong>classLoader</strong>类加载器已经被回收</p>
</li>
<li><p>对该class对象已经没有任何引用。</p>
</li>
</ul>
<p>  满足上面这些条件就可以被回收，可以发现方法区的回收条件十分的严格。</p>
<h1 id="总结："><a href="#总结：" class="headerlink" title="总结："></a>总结：</h1><p>​    本文讲述了JVM的分代模型，新生代，老年代，接着，我们对于JVM对象在分代里面分配的一些基础概念，比如对象优先分配在老年代，对象年龄晋升到老年代以及垃圾回收之后长期存活对象进入老年代，同样，JVM也存在一些特殊的判断机制让新生代提前进入老年代，这些都是十分重要的优化，在后续的系列文章中会深入讲解。</p>
<h1 id="写在最后"><a href="#写在最后" class="headerlink" title="写在最后"></a>写在最后</h1><p>​    了解分代的概念以及熟悉JVM的内存模型是非常重要的，因为现代垃圾收集器不断进化以及复杂甚至放弃分代的理念，十分有必要了解分代的历史以及分代的进程，同时不分代势必会是未来趋势。</p>
<h1 id="支付系统的调优实战"><a href="#支付系统的调优实战" class="headerlink" title="支付系统的调优实战"></a>支付系统的调优实战</h1><h2 id="支付系统的大致业务："><a href="#支付系统的大致业务：" class="headerlink" title="支付系统的大致业务："></a>支付系统的大致业务：</h2><p>​    需要处理支付的订单请求，同时需要根据商户的支付请求，从微信等平台划款到电商公司到账户去。</p>
<p>​    整个支付到交易流程如下：</p>
<ol>
<li>用户提交支付请求到商场系统</li>
<li>商场系统提交支付订单到支付系统</li>
<li>支付系统回调，让用户指定付款方式</li>
<li>用户完成支付，支付系统处理三方支付请求渠道，同时返回支付结果。</li>
</ol>
<h2 id="支付系统等性能瓶颈："><a href="#支付系统等性能瓶颈：" class="headerlink" title="支付系统等性能瓶颈："></a>支付系统等性能瓶颈：</h2><p>​    主要问题时每次创建订单都要创建一个订单对象，而百万级的创建订单就是jvm层面的主要压力。</p>
<p>​    同时引发如下问题：</p>
<ul>
<li>支付系统需要部署多少台机器</li>
<li>需要多大的内存空间</li>
<li>Jvm需要多大的堆内存</li>
<li>给jvm多大的内存空间才可以保证支撑这么多订单在内存创建，而不是导致直接崩溃</li>
</ul>
<h2 id="支付系统每分钟要处理多少订单"><a href="#支付系统每分钟要处理多少订单" class="headerlink" title="支付系统每分钟要处理多少订单"></a>支付系统每分钟要处理多少订单</h2><p>​    大约每30个订单为15kb左右的空间占用，但是一个支付接口需要创建至少数十个对象。所以总占用大概是每秒回消耗1mb的空间。</p>
<p>​    按照2核心4g的系统，jvm进程最多分配2g的空间，按照jvm分区的设置，方法区，栈，堆内存这些空间分摊之后，堆空间一般就1G多（JVM本身需要一定多空间运行）。再加上新生代老年代的分区划分，那么实际可以使用的新生代也就几百M</p>
<p>​    如果每秒创建1mb,60秒就是60M，那么基本上每过几分钟就要minor gc一次，这样会造成频繁的gc。</p>
<p>​    但是如果换成4核心8G的内存，那么jvm可以扩展到4g,可以将这个时间延长到几个小时gc一次。</p>
<h2 id="性能过小的机器，反面教材"><a href="#性能过小的机器，反面教材" class="headerlink" title="性能过小的机器，反面教材"></a>性能过小的机器，反面教材</h2><p>​    一个百万级别的系统在促销的时候，如何面对千万级的请求量。</p>
<p>​    新生代因为回收速度跟不上请求速度，导致很多请求在新生代积压，而新生代积压导致后续被移动到老年代，老年代的内存占用不断扩大。如果老年代被占满，那么会触发老年代的垃圾回收，full gc ，Full Gc会导致整个系统肉眼可见的卡顿。</p>
<h2 id="如果自己的系统突然暴增100万的交易，如何分析jvm的内存消耗"><a href="#如果自己的系统突然暴增100万的交易，如何分析jvm的内存消耗" class="headerlink" title="如果自己的系统突然暴增100万的交易，如何分析jvm的内存消耗"></a>如果自己的系统突然暴增100万的交易，如何分析jvm的内存消耗</h2><p>​    不管自己的系统多大或者多小，都要严格去分析一下扩大100倍之后的情况是什么？</p>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kity@2.0.4/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kityminder-core@1.4.50/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.css">
    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>阿东
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://whitestore.top/2021/08/17/202201282354/" title="深入理解JVM - 分代的基本概念">https://whitestore.top/2021/08/17/202201282354/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLzQuMC96aC1DTg=="><i class="fa fa-fw fa-creative-commons"></i>BY-NC</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/JVM/" rel="tag"># JVM</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/08/17/sixmaozi/" rel="prev" title="《六项思考帽》读书笔记">
      <i class="fa fa-chevron-left"></i> 《六项思考帽》读书笔记
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/08/17/weixiguan/" rel="next" title="《微习惯》读书笔记">
      《微习惯》读书笔记 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#深入理解JVM-分代的基本概念"><span class="nav-number">1.</span> <span class="nav-text">深入理解JVM - 分代的基本概念</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#目录"><span class="nav-number">2.</span> <span class="nav-text">目录</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#前言"><span class="nav-number">3.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#概述："><span class="nav-number">4.</span> <span class="nav-text">概述：</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#JVM的分代模型"><span class="nav-number">4.1.</span> <span class="nav-text">JVM的分代模型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#对象分配的基础了解"><span class="nav-number">4.2.</span> <span class="nav-text">对象分配的基础了解</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#长期存活的对象会躲过垃圾回收"><span class="nav-number">4.3.</span> <span class="nav-text">长期存活的对象会躲过垃圾回收</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#jvm内存的核心参数："><span class="nav-number">4.4.</span> <span class="nav-text">jvm内存的核心参数：</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#启动参数的设置？"><span class="nav-number">4.4.1.</span> <span class="nav-text">启动参数的设置？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Tomcat和spring-boot启动的时候参数如何设置"><span class="nav-number">4.4.2.</span> <span class="nav-text">Tomcat和spring boot启动的时候参数如何设置</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#方法区的对象到底会不会被回收？"><span class="nav-number">4.5.</span> <span class="nav-text">方法区的对象到底会不会被回收？</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#总结："><span class="nav-number">5.</span> <span class="nav-text">总结：</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#写在最后"><span class="nav-number">6.</span> <span class="nav-text">写在最后</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#支付系统的调优实战"><span class="nav-number">7.</span> <span class="nav-text">支付系统的调优实战</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#支付系统的大致业务："><span class="nav-number">7.1.</span> <span class="nav-text">支付系统的大致业务：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#支付系统等性能瓶颈："><span class="nav-number">7.2.</span> <span class="nav-text">支付系统等性能瓶颈：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#支付系统每分钟要处理多少订单"><span class="nav-number">7.3.</span> <span class="nav-text">支付系统每分钟要处理多少订单</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#性能过小的机器，反面教材"><span class="nav-number">7.4.</span> <span class="nav-text">性能过小的机器，反面教材</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#如果自己的系统突然暴增100万的交易，如何分析jvm的内存消耗"><span class="nav-number">7.5.</span> <span class="nav-text">如果自己的系统突然暴增100万的交易，如何分析jvm的内存消耗</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">阿东</p>
  <div class="site-description" itemprop="description">随遇而安</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">239</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">36</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">37</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2xhenlUaW1lcw==" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;lazyTimes"><i class="fa fa-fw fa-github"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="bWFpbHRvOjEwOTc0ODM1MDhAcXEuY29t" title="E-Mail → mailto:1097483508@qq.com"><i class="fa fa-fw fa-envelope"></i>E-Mail</span>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly93d3cuNTJwb2ppZS5jbi9ob21lLnBocD9tb2Q9c3BhY2UmdWlkPTE0OTc3MTgmZG89dGhyZWFkJnZpZXc9bWUmZnJvbT1zcGFjZQ==" title="https:&#x2F;&#x2F;www.52pojie.cn&#x2F;home.php?mod&#x3D;space&amp;uid&#x3D;1497718&amp;do&#x3D;thread&amp;view&#x3D;me&amp;from&#x3D;space">吾爱破解</span>
        </li>
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly9qdWVqaW4uaW0vdXNlci8yOTk5MTIzNDUyNjI2MzY2" title="https:&#x2F;&#x2F;juejin.im&#x2F;user&#x2F;2999123452626366">掘金</span>
        </li>
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly9zZWdtZW50ZmF1bHQuY29tL3UvbGF6eXRpbWVz" title="https:&#x2F;&#x2F;segmentfault.com&#x2F;u&#x2F;lazytimes">思否</span>
        </li>
    </ul>
  </div>

      </div>

      <div class="wechat_OA">
        <span>欢迎关注我的公众号</span>
        <br>
          <!-- 这里添加你的二维码图片 -->
        <img src ="https://adong-picture.oss-cn-shenzhen.aliyuncs.com/adong/wechat_channel.jpg">
      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">阿东</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">2m</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">29:50</span>
</div>
  <div class="powered-by">由 <span class="exturl theme-link" data-url="aHR0cHM6Ly9oZXhvLmlv">Hexo</span> & <span class="exturl theme-link" data-url="aHR0cHM6Ly90aGVtZS1uZXh0Lm9yZw==">NexT.Gemini</span> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : true,
      appId      : 'qMUpEEvBgXaMDD1b0ftgi9xr-gzGzoHsz',
      appKey     : 'UCdfT4Rfih6MO6y8DI4fstf6',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-CN' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
